package net.lzzy.algorithm;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import net.lzzy.algorithm.algorlib.BaseSort;
import net.lzzy.algorithm.algorlib.DirectSort;
import net.lzzy.algorithm.algorlib.InsertSort;
import net.lzzy.algorithm.algorlib.ShellSort;
import net.lzzy.algorithm.algorlib.SortFactory;
import net.lzzy.algorithm.find.BaseSearch;
import net.lzzy.algorithm.find.SearchFactory;

import java.util.Locale;
import java.util.Objects;
import java.util.Random;

/**
 * @author Administrator
 */
public class MainActivity extends AppCompatActivity  {
    private Integer[] items;
    private EditText edtItems;
    private TextView tvResult;
    Spinner spinner1;
    Spinner spinner;
    Button btnSort1;
    Button btnSort2;
    Button btnSort3;
    LinearLayout container;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtItems = findViewById(R.id.activity_main_edt_items);
        btnSort3=findViewById(R.id.activity_main_btn_find);
        btnSort1=findViewById(R.id.activity_main_btn_generate);
        btnSort2=findViewById(R.id.activity_main_btn_sort);
//        btnSort.setOnClickListener(this);
        tvResult = findViewById(R.id.activity_main_tv_result);
        container = findViewById(R.id.line);
        //======================下拉框的绑定与适配器的安装=========================
        //======================排序方法的适配器绑定==============================
        final ArrayAdapter<String> adapter = new ArrayAdapter<>
                (this, android.R.layout.simple_dropdown_item_1line, SortFactory.getsortName());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner = findViewById(R.id.activity_main_pull);
        spinner.setAdapter(adapter);
        //======================查找方法的适配器绑定=================================
        final ArrayAdapter<String> adapter1 = new ArrayAdapter<>
                (this, android.R.layout.simple_dropdown_item_1line, SearchFactory.getsSearchName());
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1 = findViewById(R.id.activity_main_pull2);
        spinner1.setAdapter(adapter1);


//======================生成随机数==================
        btnSort1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                generateItems();
                displayItems(edtItems);
            }
        });
//     ====================排序数据=========================
        btnSort2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseSort<Integer> sort = SortFactory.getInstance(spinner.getSelectedItemPosition(), items);
                BaseSort<Integer> sortNotNull = Objects.requireNonNull(sort);
                sortNotNull.sortTime();
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("排序成功");
                builder.setMessage("对比次数：" + sort.getCompareCount() + "\n"
                        + "移动次数：" + sort.getCompareMove() + "\n" + "交换次数："
                        + sort.getCompareSwop() + "\n" + "运算时长: " + sort.getTime() + "毫秒").show();
                displayItems(tvResult);
            }
        });
        //========================生成查找数据========================
        btnSort3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             resetSearch();
            }
        });
    }
//    @Override
//    public void onClick(View v) {
//       switch (v.getId()){
//           case R.id.activity_main_btn_generate:
//               generateItems();
//               displayItems(edtItems);
//               break;
////           case R.id.activity_main_btn_sort:
////               BaseSort<Integer> sort = SortFactory.getInstance(spinner.getSelectedItemPosition(), items);
////               BaseSort<Integer> sortNotNull = Objects.requireNonNull(sort);
////               sortNotNull.sortTime();
////               AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
////               String result=sortNotNull.getResult();
////               tvResult.setText(result);
////               builder.setTitle("排序成功");
////               builder.setMessage("对比次数：" + sort.getCompareCount() + "\n"
////                       + "移动次数：" + sort.getCompareMove() + "\n" + "交换次数："
////                       + sort.getCompareSwop() + "\n" + "运算时长: " + sort.getTime() + "毫秒").show();
////               break;
//           case R.id.activity_main_btn_find:
//               resetSearch();
//               break;
//       }
//    }
        //==============在显示框中显示点击中的数据所在的位置====================
        private View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseSearch<Integer> search = SearchFactory.getInstance(spinner1.getSelectedItemPosition(), items);
                if (search != null) {
                    int pos = search.search(v.getId());
                    tvResult.setText("该元素位于".concat((pos + 1) + "位"));
                }
            }
        };
        //===================点击生成查找数据并且在点击生成的数据时显示出点击数据的当前位置=============
        private void resetSearch() {
            container.removeAllViews();
            generateItems();
            if (spinner1.getSelectedItemPosition()==2){
                btnSort1.callOnClick();
            }
            for (Integer i : items) {
                Button btn = new Button(this);
                btn.setText(String.format(i.toString(), Locale.CHINA));
                btn.setId(i);
                btn.setLayoutParams(new LinearLayout.LayoutParams(0,
                        ViewGroup.LayoutParams.WRAP_CONTENT, 1));
                btn.setOnClickListener(listener);
                container.addView(btn);
            }
        }
        //=====================生成随机数并显示出随机数===========================
        private void displayItems (TextView tv){
            String display = "";
            for (Integer i : items) {
                display = display.concat(i + ",");
            }
            display = display.substring(0, display.length() - 1);
            tv.setText(display);
        }
        //=======================生成随机数==========================
        private void generateItems () {
            items = new Integer[10];
            Random generator = new Random();
            for (int i = 0; i < items.length; i++) {
                items[i] = generator.nextInt(99);
            }
        }
}